package com.dxtx.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.dxtx.common.R;

/**
 * Created by user on 2016/4/27.
 */
public class MyRecyclerView extends RecyclerView {
    private int selectPosition;
    private RectF rect = new RectF();
    private Paint paint;
    private float rx;

    public MyRecyclerView(Context context) {
        super(context);
    }

    public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        addItemDecoration(new DividerLine(context, attrs));
        init();
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        int expandSep = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(expandSep, heightSpec);
    }

    private void init() {
        rx = getResources().getDimension(R.dimen._5dp);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(getResources().getColor(R.color.colorPrimary));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(rx / 3);
    }

    private boolean initFinish = false;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (!initFinish) {
            View view = getChildAt(selectPosition);
            rect.set(view.getLeft() + rx * 2, view.getTop() + rx * 2, view.getRight() - rx * 2, view.getBottom() - rx * 2);
            invalidate();
            initFinish = true;
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawRoundRect(rect, rx / 2, rx / 2, paint);
    }

    private RectF sF = new RectF(), nF = new RectF();
    private ValueAnimator animator;
    private ValueAnimator.AnimatorUpdateListener updater = new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float v = (Float) animation.getAnimatedValue();
            rect.left = (nF.left - sF.left) * v + sF.left;
            rect.right = (nF.right - sF.right) * v + sF.right;
            invalidate();
        }
    };

    public void smoothIndicator(int postion) {
        if (selectPosition != postion) {
            if (animator != null) {
                animator.cancel();
            }
            animator = ValueAnimator.ofFloat(0, 1);
            sF.set(rect);
            View v = getChildAt(postion);
            nF.set(v.getLeft() + rx * 2, rect.top, v.getRight() - rx * 2, rect.bottom);
            animator.addUpdateListener(updater);
            animator.setDuration(250);
            animator.start();
            selectPosition = postion;
        }
    }
}
